package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.MemberDao;
import ysq.xyg.model.Member;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("memberService")
public class MemberServiceImpl extends BaseServiceImpl<Member> implements MemberService {
    @Resource
    private MemberDao memberDao;
    @Autowired
    public void setMemberDao(MemberDao memberDao) {
        this.baseDao = memberDao;
    }

    //分页查询
    public PageInfo<Member> pageInfo(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(member!=null){
            if(StringUtil.isNotEmpty(member.getNickName())){
                criteria.andLike("nickName","%"+ BaseUtil.encodeBase64String(member.getNickName())+"%");
            }
            if(StringUtil.isNotEmpty(member.getPhone())){
                criteria.andLike("phone","%"+member.getPhone()+"%");
            }
            if(StringUtil.isNotEmpty(member.getType())){
                criteria.andEqualTo("type",member.getType());
            }
        }
        List<Member> list=memberDao.selectByExample(example);
        return new PageInfo<Member>(list);
    }

    public PageInfo<Member> pageInfoLeft(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Member> list=memberDao.selectListLeft(member);
        return new PageInfo<Member>(list);
    }
    public List<Member> listLeft(Member member) {
        List<Member> list=memberDao.selectListLeft(member);
        return list;
    }
    public Member selectById(String id) {
        Member member=new Member();
        member.setOpenid(id);
        List<Member> list=memberDao.selectListLeft(member);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
    public int selectMemberCount(Member member){
        return memberDao.selectMemberCount(member);
    }
}
